package cn.caishen.report.service.report.impl;

import cn.caishen.report.common.constant.LbConstant;
import cn.caishen.report.common.exceptions.BusinessException;
import cn.caishen.report.common.utils.LbMap;
import cn.caishen.report.dao.report.ReportUserFieldDao;
import cn.caishen.report.domain.report.ReportUserField;
import cn.caishen.report.service.report.ReportUserFieldService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 报表用户字段表Service实现类
 *
 * @author Administrator
 */
@Service("reportUserFieldService")
@Transactional(rollbackFor = Throwable.class)
public class ReportUserFieldServiceImpl extends ServiceImpl<ReportUserFieldDao, ReportUserField> implements ReportUserFieldService {

    private final ReportUserFieldDao reportUserFieldDao;

    public ReportUserFieldServiceImpl(ReportUserFieldDao reportUserFieldDao) {
        this.reportUserFieldDao = reportUserFieldDao;
    }

    /**
     * 分页查询列表
     *
     * @param page  页数
     * @param limit 条数
     * @param param 参数
     * @return PageInfo
     */
    @Override
    public PageInfo<ReportUserField> getList(int page, int limit, LbMap param) {
        //UserVo user = LoginInformation.getUser();
        QueryWrapper<ReportUserField> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", LbConstant.DELETE_STATUS.UN_DELETE.getCode()).eq("report_config_id", param.getInt("reportConfigId"));
        List<ReportUserField> list = reportUserFieldDao.selectList(wrapper);
        return new PageInfo<ReportUserField>(list);
    }

    /**
     * 查询所有列表
     *
     * @param param 参数
     * @return list 全部列表
     */
    @Override
    public List<ReportUserField> getAllList(LbMap param) {
        QueryWrapper<ReportUserField> wrapper = new QueryWrapper<>();
        return reportUserFieldDao.selectList(wrapper);
    }

    /**
     * 通过编号查询
     *
     * @param primaryKey
     * @return 报表用户字段
     */
    @Override
    @SneakyThrows
    public ReportUserField getById(String primaryKey) {
        ReportUserField reportUserField = reportUserFieldDao.selectById(primaryKey);
        if (Objects.isNull(reportUserField)) {
            throw new BusinessException("报表用户字段编号不正确");
        }
        return reportUserField;
    }

    /**
     * 新增信息
     *
     * @param reportUserField reportUserField
     */
    @Override
    @SneakyThrows
    public void saveOne(ReportUserField reportUserField) {
        //UserVo user = LoginInformation.getUser();
        reportUserField.setUserId(0);
        reportUserField.setUserName("");
        reportUserField.setDeleteFlag(LbConstant.DELETE_STATUS.UN_DELETE.getCode());
        int count = reportUserFieldDao.insert(reportUserField);
        if (count != 1) {
            throw new BusinessException("报表用户字段保存失败");
        }
    }

    /**
     * 更新信息
     *
     * @param reportUserField reportUserField
     */
    @Override
    @SneakyThrows
    public void update(ReportUserField reportUserField) {
        int count = reportUserFieldDao.updateById(reportUserField);
        if (count != 1) {
            throw new BusinessException("报表用户字段更新失败");
        }
    }

    /**
     * 删除信息
     *
     * @param primaryKey id
     */
    @Override
    @SneakyThrows
    public void delete(String primaryKey) {
        int count = reportUserFieldDao.deleteById(primaryKey);
        if (count != 1) {
            throw new BusinessException("报表用户字段删除失败");
        }
    }

    /**
     * 更新用户自定义列
     *
     * @param reportConfigId
     * @param reportUserFieldList
     */
    @Override
    @SneakyThrows
    public void saveUserField(Integer reportConfigId, List<ReportUserField> reportUserFieldList) {
        List<ReportUserField> oldReportUserFieldList = reportUserFieldDao.selectList(new QueryWrapper<ReportUserField>().eq("report_config_id", reportConfigId).eq("user_id", 0).eq("unit_id", 0));
        for (ReportUserField reportUserField : oldReportUserFieldList) {
            Integer reportUserFieldId = reportUserField.getReportUserFieldId();
            int count = reportUserFieldDao.deleteById(reportUserFieldId);
            if (count != 1) {
                throw new BusinessException("报表用户字段删除失败");
            }
        }

        for (ReportUserField reportUserField : reportUserFieldList) {
            reportUserField.setReportConfigId(reportConfigId);
            this.saveOne(reportUserField);
        }
    }
}
